package com.bai.Service.Impl;

import com.bai.Constant.ResultConst;
import com.bai.Mapper.PostMapper;
import com.bai.Pojo.Post;
import com.bai.Service.PostService;
import com.bai.Utils.DtoResultUtil;
import com.bai.dto.PageLimitDto;
import com.bai.dto.PublicResponseBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

@Transactional
public class PostServiceImpl implements PostService {
    public void setPostMapper(PostMapper postMapper) {
        this.postMapper = postMapper;
    }

    @Autowired
    private PostMapper postMapper;

    @Override
    public PublicResponseBody addPost(Post post) {
        int addPost = postMapper.addPost(post);
        if (addPost>0){
            return DtoResultUtil.resultSuccess();
        }else {
            return DtoResultUtil.result(-1, ResultConst.add_error);
        }
    }

    @Override
    public PublicResponseBody UpdatePost(Post post) {
        int updatePost = postMapper.UpdatePost(post);
        if (updatePost>0){
            return DtoResultUtil.resultSuccess();
        }else {
            return DtoResultUtil.result(-1,ResultConst.edit_error);
        }
    }

    @Override
    public PublicResponseBody deletePostById(Integer id) {
        int deletePostById = postMapper.deletePostById(id);
        if (deletePostById>0){
            return DtoResultUtil.resultSuccess();
        }else {
            return DtoResultUtil.result(-1, ResultConst.delete_error);
        }
    }

    @Override
    public PageLimitDto QueryAll(Integer offset, Integer limit) {
        PageLimitDto pageLimitDto=new PageLimitDto();
        pageLimitDto.setTotal(postMapper.queryAllPostCount());
        pageLimitDto.setRows(postMapper.QueryAll(offset,limit));
        return pageLimitDto;
    }

    @Override
    public PublicResponseBody getPost(Integer id) {
        Post post = postMapper.getPost(id);
        if (post!=null){
            return DtoResultUtil.resultSuccess(post);
        }else {
            return DtoResultUtil.result(-1,ResultConst.query_error);
        }
    }
}
